<?php

namespace App\Http\ViewComposer;


use App\Models\AdminMenusModel;
use App\Models\AdminRolesModel;
use App\Models\AdminUsersModel;
use Illuminate\View\View;

class PermissionComposer
{
    public function compose(View $view)
    {
        $modules_path = [];
        $modules = [];
        $menus = [];
        $firstModule = "";
        $path = request()->path();
        $uid = session()->get('userinfo.id');
        $user = AdminUsersModel::getAdminUsersData(['id' => $uid]);

        //根据管理员id以及角色获取不同的菜单
        if ($user->roles == AdminUsersModel::SUPERUSERS) { //超级管理员可以看到所有菜单，包括隐藏的菜单
            $menus = AdminMenusModel::getAllAdminMenusForLeftMenu(['is_not_btn' => AdminMenusModel::BUTTON]);
            $info = AdminMenusModel::getAdminMenusData(['rule' => $path]);
            $btns = array_map('get_object_vars', AdminMenusModel::getAllButtons($path, true)); //按钮
        } else {
            $premission = "";
            $premissionIds = [];
            $role = AdminRolesModel::getAdminRoleData(['id' => $user->roles, 'status' => AdminRolesModel::ROLEENABLE]);
            if (!is_null($role)) {
                $premission = $role->premission;
            }
            if (!empty($premission)) {
                if ($premission !== AdminRolesModel::SUPERROLE) {
                    if (strpos($premission, ",") !== false) {
                        $premissionIds = array_filter(explode(",", $premission));
                    } else {
                        $premissionIds = array($premission);
                    }
                }
                $menus = AdminMenusModel::getAllAdminMenusForLeftMenu([
                    'is_not_btn' => AdminMenusModel::BUTTON,
                    'status' => AdminMenusModel::MENUSHOW,
                    'id' => $premissionIds
                ]);
            }
            $info = AdminMenusModel::getAdminMenusData(['rule' => $path, 'status' => AdminMenusModel::MENUSHOW]);
            $btns = array_map('get_object_vars', AdminMenusModel::getAllButtons($path, false)); //按钮
        }

        if (!empty($menus)) {
            $firstModule = array_values($menus)[0]['rule'];
            foreach ($menus as $key => $menu) {
                //每个模块默认跳转的地址
                $default_path = !empty($menu['child'][0]['child'][0]['rule']) ? $menu['child'][0]['child'][0]['rule'] : "405";
                if (!empty($default_path)) {
                    $modules[] = [
                        'name' => $menu['name'],
                        'rule' => $menu['rule'],
                        'default_path' => $default_path
                    ];
                }
                //所有菜单栏目对应的模块
                if (!empty($menu['child'])) {
                    foreach ($menu['child'] as $key => $child) {
                        if (!empty($child['child'])) {
                            foreach ($child['child'] as $k => $value) {
                                $modules_path[$value['rule']] = $menu['rule'];
                            }
                        }
                    }
                }
            }
        }
        $default_module = !empty($modules_path[$path]) ? $modules_path[$path] : $firstModule;  //默认模块
        $premisson_menus = !empty($menus[$default_module]) ? $menus[$default_module] : array();
        $view->with([
            'module' => $modules,
            'permission_menus' => $premisson_menus,
            'default_module' => $default_module,
            'path' => $path,
            'btns' => !empty($btns) ? array_column($btns, 'name', 'rule') : array(),
            'default_name' => !is_null($info) ? $info->name : ""
        ]);
    }


}
